package org.example.everyday;

/**
 * <p>
 * Description:TODO 详细描述
 * <p>
 * <p>
 *
 * @author anlu
 * @date 2022/4/17 0017 22:37
 */
public class Lc125 {
    public static void main(String[] args) {
        Lc125 lc125 = new Lc125();
        String s = "A man, a plan, a canal: Panama";
        boolean res =  lc125.isPalindrome(s);
        System.out.println(res);

    }
    public boolean isPalindrome(String s) {
        if(s == null){
            return false;
        }

        int left = 0,right = s.length() - 1;
        while(left < right){
            while(left < right && !isValid(s.charAt(left))){
                left++;
            }
            while(left < right && !isValid(s.charAt(right))){
                right--;
            }
            if(left < right && !isEquals(s.charAt(left),s.charAt(right))){
                return false;
            }
            left++;
            right--;
        }
        return true;
    }

    private boolean isValid(char c){
        return Character.isLetter(c) || Character.isDigit(c);
    }

    private boolean isEquals(char a, char b){
        return Character.toLowerCase(a) == Character.toLowerCase(b);
    }
}
